/*
 * ValidatorWindow.java
 *
 * Created on 24-Feb-2013, 2:27:03 PM
 */
package ngram_reader.validator;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.prefs.Preferences;
import javax.swing.JFileChooser;
import javax.swing.SwingWorker;
import main.Main;
import ngram.util.GuiUtil;
import swingExtended.preferences.PreferenceGroup;
import swingExtended.progressPanel.ProgressPanel;
import swingExtended.progressPanel.ProgressPanelListener;
import swingExtended.progressPanel.ProgressPanelWorkerFactory;

/**
 *
 * @author Shimu
 */
public class ValidatorWindow extends javax.swing.JFrame {
       
       
    /** Creates new form ValidatorWindow */
    public ValidatorWindow() {
        initComponents();
        this.setLocationRelativeTo(this.getParent());
        
        // Get the preferences
        final PreferenceGroup PREF_GROUP = new PreferenceGroup(Preferences.userRoot().node(
                Main.USER_PREF_ROOT_NODE + "/" + this.getClass()));
        
        inputBrowserPanel.getFileChooser().setFileSelectionMode(
                JFileChooser.DIRECTORIES_ONLY);
                
        PREF_GROUP.add(inputBrowserPanel, "input", "");
        PREF_GROUP.add(checkBoxDeleteCorruptedFiles, "deleteCorrupt");
        PREF_GROUP.add(checkBoxValidateAllFiles, "validateAll");
                
        PREF_GROUP.loadPreferences();
        
        progressPanel.addListener(new ProgressPanelListener() {

            @Override
            public void onStart(ProgressPanel emitter) {
                inputBrowserPanel.setEnabled(false);
                checkBoxDeleteCorruptedFiles.setEnabled(false);
                checkBoxValidateAllFiles.setEnabled(false);
            }

            @Override
            public void onStop(ProgressPanel emitter) {
                inputBrowserPanel.setEnabled(true);
                checkBoxDeleteCorruptedFiles.setEnabled(true);
                checkBoxValidateAllFiles.setEnabled(true);
            }
        });
        this.progressPanel.addWorkerFactory(new ProgressPanelWorkerFactory() {

            @Override
            public SwingWorker CreateWorker() {
                        return new Validator_Version2(ValidatorWindow.this.progressPanel,
                                inputBrowserPanel.getFile(),
                                checkBoxDeleteCorruptedFiles.isSelected(),
                                checkBoxValidateAllFiles.isSelected());
            }
        });
        
        this.addWindowListener(new WindowAdapter(){

            @Override
            public void windowClosed(WindowEvent e) {
                PREF_GROUP.savePreferences();
            }
        });
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        inputBrowserPanel = new swingExtended.inputBrowsePanel.InputBrowserPanel("Input Folder:");
        progressPanel = new swingExtended.progressPanel.ProgressPanel();
        jPanel1 = new javax.swing.JPanel();
        checkBoxDeleteCorruptedFiles = new javax.swing.JCheckBox();
        checkBoxValidateAllFiles = new javax.swing.JCheckBox();
        labelOptions = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(-16777216,true)));

        checkBoxDeleteCorruptedFiles.setSelected(true);
        checkBoxDeleteCorruptedFiles.setText("Delete Corrupted Files");
        checkBoxDeleteCorruptedFiles.setToolTipText("Select the checkbox if you wish to make the validator delete all files that are found to be corrupted.");
        checkBoxDeleteCorruptedFiles.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                checkBoxDeleteCorruptedFilesActionPerformed(evt);
            }
        });

        checkBoxValidateAllFiles.setText("Validate All Files");
        checkBoxValidateAllFiles.setToolTipText("If checked, the validator will attempt to validate all the files. If unchecked, the validator will attempt to look for a file containing a list of files already validated and ignore those during validation.");

        labelOptions.setText("Options");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelOptions)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(checkBoxValidateAllFiles)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(checkBoxDeleteCorruptedFiles)))
                .addContainerGap(322, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(16, 16, 16)
                .addComponent(labelOptions)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(checkBoxValidateAllFiles)
                    .addComponent(checkBoxDeleteCorruptedFiles))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(progressPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 568, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(inputBrowserPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 568, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(inputBrowserPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(progressPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 437, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(17, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void checkBoxDeleteCorruptedFilesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkBoxDeleteCorruptedFilesActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_checkBoxDeleteCorruptedFilesActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox checkBoxDeleteCorruptedFiles;
    private javax.swing.JCheckBox checkBoxValidateAllFiles;
    private swingExtended.inputBrowsePanel.InputBrowserPanel inputBrowserPanel;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel labelOptions;
    private swingExtended.progressPanel.ProgressPanel progressPanel;
    // End of variables declaration//GEN-END:variables
}
